#include <cassert>
#include <iostream>
#include "SphereCollider.hpp"
#include "../object/GameObject.hpp"

SphereCollider::SphereCollider(GameObject* parent, float radius) : Collider(parent), radius(radius) {

}

SphereCollider::~SphereCollider() {
    //dtor
}

float SphereCollider::getRadius() {
    return radius;
}

bool SphereCollider::rayIntersection(CollisionRay* ray, float& t) {
    glm::vec3 startToCenter = parent->getLocation() - ray->getStartPosition();
    float distance = glm::dot(startToCenter, ray->getDirection());

    if(distance < 0.0f) {
        distance = 0.0f;
    }

    glm::vec3 point = ray->getStartPosition() + ray->getDirection() * distance;

    if(glm::length2(parent->getLocation() - point) < radius * radius) {
        t = distance;
        return true;
    }

    return false;
}
